#include "transferprogress.h"
#include "ui_transferprogress.h"
#include "common.h"

#include <QTimer>

static int updateTime = 500;

TransferProgress::TransferProgress(QWidget *parent) :
    QWidget(parent),
	ui(new Ui::TransferProgress),
	m_TransferredSize(0),
	m_LastTransferredSize(0)
{
    ui->setupUi(this);
	ui->transferProgress->setRange(0, 100);
	ui->transferProgress->setValue(0);

	QTimer* timer = new QTimer(this);
	timer->setInterval(updateTime);
	connect(timer, SIGNAL(timeout()),
			this, SLOT(updateSpeed()));
	timer->start();
}

TransferProgress::~TransferProgress()
{
    delete ui;
}

void TransferProgress::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void TransferProgress::setFileName(const QString &fileName)
{
	ui->fileNameLabel->setText(fileName);
}

void TransferProgress::setFileSize(long long fileSize)
{
	ui->fileSizeLabel->setText( HumanReadableSize(fileSize) );
	m_FileSize = fileSize;
}

void TransferProgress::setTransferredSize(long long transferredSize)
{
	ui->transferredLabel->setText(HumanReadableSize(transferredSize));
	m_TransferredSize = transferredSize;
	if(m_FileSize != 0)
		ui->transferProgress->setValue( (float)transferredSize / m_FileSize * 100 );
	else
		ui->transferProgress->setValue(100);
}

void TransferProgress::updateSpeed()
{
	int sendSize = m_TransferredSize - m_LastTransferredSize;
	m_LastTransferredSize = m_TransferredSize;
	ui->speedLabel->setText( HumanReadableSize(sendSize  / updateTime * 1000) + "/S");
}
